package com.home.cop.utils;

import java.security.MessageDigest;

public class MD5Utils {
    public static String MD5(String key){
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] bytes = md.digest(key.getBytes("utf-8"));
            final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();
            StringBuilder ret = new StringBuilder(bytes.length * 2);
            for (int i=0; i<bytes.length; i++) {
                ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
                ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
            }
            return ret.toString();
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String MD5Uppercase(String key){
        return MD5Utils.MD5(key).toUpperCase();
    }

    public static void main(String[] args) {
        System.out.println(MD5Utils.MD5("123456"));
        System.out.println(MD5Utils.MD5Uppercase("111111"));
    }
}
